var IDValidator = require('./IDValidator')
var GB2260 = require('./GB2260')

// valid identify
const validID = id => {
  const Validator = new IDValidator( GB2260 )
  return Validator.isValid(id)
}

// valid phone
const validPhone = phone => {
  const reg = /^1\d{10}$/
  return reg.test(phone)
}

// valid email
const validEmail = email => {
  const reg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/
  return reg.test(email)
}

// valid user name
const validUserName = userName => {
  if (userName && userName.length > 0 && userName.length < 8) {
    return true
  }
  return false
}

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}


const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime,
  validID,
  validPhone,
  validEmail,
  validPhone,
  validUserName
}
